-----------------------------------------------------------------------------
--
-- Module      :  Data.BitField
-- Copyright   :  Mikhail Belyaev
-- License     :  GPL2
--
-- Maintainer  :
-- Stability   :
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------

module Data.BitField  where

import Data.Ix
import Data.Bits (Bits(..))

data BitSize = Infinite | Sized Integer

class BitField a where
    getbfBit :: Integer -> a -> Bool
    setbfBit :: Integer -> a -> Bool -> a
    mapBits :: (BitField b) => (Integer,Integer) -> a -> b -> b
    mapBits (from,to) x y = let iter f t ret | f < t = iter (f+1) t (setbfBit f ret $ getbfBit f x)
                                iter f t ret | f == t = ret
                            in iter from to y


instance BitField Integer where
    getbfBit ix val = testBit val $ fromInteger ix
    setbfBit ix val True = setBit val $ fromInteger ix
    setbfBit ix val False = clearBit val $ fromInteger ix

